package com.suguowen.sblog.controller;

import com.suguowen.sblog.bean.Tag;
import com.suguowen.sblog.service.HomeService;
import com.suguowen.sblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TagController {

    @Autowired
    HomeService showService;
    @Autowired
    TagService tagService;

    @RequestMapping("/toTag")
    public String toTag(Model model) {
        // 获得tag栏
        //model.addAttribute("tags", showService.findAllTag());
        // 获得各个tag的文章数量
        List<Tag> tagedArts = tagService.findContainTagedArtsCountTag();
        model.addAttribute("tags_detail",tagedArts);
        return "tag";
    }
    @RequestMapping("/createTag")
    @ResponseBody
    public void createTag(@RequestParam("tagName")String tagName){
        Tag tag = new Tag();
        tag.setName(tagName);
        tagService.createTag(tag);
    }
    @RequestMapping("/tagModify")
    @ResponseBody
    public void modifyTag(@RequestParam("tagId")Integer tagId,
                          @RequestParam("tagName")String tagName){
        // 数据持久化
        Tag tag = new Tag();
        tag.setId(tagId);
        tag.setName(tagName);
        tagService.modifyTag(tag);
    }
    @RequestMapping("/deleteTag")
    @ResponseBody
    public void deleteTag(@RequestParam("tagId")Integer tagId){
        tagService.deleteTag(tagId);
    }
}
